package com.app.service.impl;

import com.app.mapper.jiuquan.SpecialityManageMapper;
import com.app.mapper.jiuquan.TrainMapper;
import com.app.mapper.jiuquan.TrainSpecialityMapper;
import com.app.model.SpecialityManageModel;
import com.app.model.TrainModel;
import com.app.model.TrainSpecialityModel;
import com.app.service.TrainSpecialityService;
import com.app.vo.TrainSpecialityVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainSpecialityServiceImpl extends ServiceImpl<TrainSpecialityMapper, TrainSpecialityModel> implements TrainSpecialityService {
    @Autowired
    private TrainSpecialityMapper trainSpecialityMapper;

    @Autowired
    private SpecialityManageMapper specialityManageMapper;

    @Autowired
    private TrainMapper trainMapper;

    //查询这个培训机构下的所有专业
    @Override
    public TrainSpecialityVo getTrainSpecialityList(Integer page, Integer limit, Integer trainId,Integer type) {
        Page<TrainSpecialityModel> page1 = new Page<>(page, limit);
        QueryWrapper<TrainSpecialityModel> qw = new QueryWrapper<>();
        qw.eq("train_id", trainId);
        if(type != null && type != 0)
        {
            qw.eq("train_type", type);
        }
        Page<TrainSpecialityModel> trainSpecialityPage = trainSpecialityMapper.selectPage(page1, qw);

        List<TrainSpecialityModel> trainSpecialityList = trainSpecialityPage.getRecords();
        for (TrainSpecialityModel trainSpeciality : trainSpecialityList) {
            //设置培训机构名称
            Integer specialityId = trainSpeciality.getSpecialityId();
            TrainModel trainModel = trainMapper.selectById(trainSpeciality.getTrainId());
            trainSpeciality.setTrainIdText(trainModel.getTrainInstitution());
            //设置专业名称
            SpecialityManageModel specialityManageModel = specialityManageMapper.selectById(specialityId);
            if(specialityManageModel!=null)
            {
                trainSpeciality.setSpecialityText(specialityManageModel.getName());
            }

            //设置培训类型名称 1 创业培训  2 技能培训
            Integer trainType = trainSpeciality.getTrainType();
            if (trainType == 1) {
                trainSpeciality.setTrainTypeText("创业培训");
            }
            if (trainType == 2) {
                trainSpeciality.setTrainTypeText("技能培训");
            }

        }
        long count = trainSpecialityPage.getTotal();

        TrainSpecialityVo trainSpecialityVo = new TrainSpecialityVo();
        trainSpecialityVo.setList(trainSpecialityList);
        trainSpecialityVo.setCount((int) count);
        return trainSpecialityVo;
    }

    @Override
    public TrainSpecialityVo getUserTrainSpecialityList(Integer page,Integer limit,Integer trainId) {

        Page<TrainSpecialityModel> page1 = new Page<>(page, limit);
        QueryWrapper<TrainSpecialityModel> trainType1 = new QueryWrapper<>();
        trainType1.eq("train_id", trainId);
        trainType1.eq("train_type", 1);
        Page<TrainSpecialityModel> entrepreneurshipTrainPage = trainSpecialityMapper.selectPage(page1, trainType1);
        List<TrainSpecialityModel> entrepreneurshipTrainList = entrepreneurshipTrainPage.getRecords();
        for (TrainSpecialityModel entrepreneurshipTrain : entrepreneurshipTrainList) {
            entrepreneurshipTrain.setTrainTypeText("创业培训");
        }
        TrainSpecialityVo trainSpecialityVo = new TrainSpecialityVo();


        QueryWrapper<TrainSpecialityModel> trainType2 = new QueryWrapper<>();
        trainType2.eq("train_id", trainId);
        trainType2.eq("train_type", 2);
        Page<TrainSpecialityModel> skillTrainPage = trainSpecialityMapper.selectPage(page1, trainType2);
        List<TrainSpecialityModel> skillTrainList = skillTrainPage.getRecords();

        for (TrainSpecialityModel skillTrain : skillTrainList) {
            skillTrain.setTrainTypeText("技能培训");
        }

        trainSpecialityVo.setEntrepreneurshipTrain(entrepreneurshipTrainList);
        trainSpecialityVo.setSkillTrain(skillTrainList);
        trainSpecialityVo.setCount(entrepreneurshipTrainList.size()+skillTrainList.size());
        return trainSpecialityVo;
    }
}
